<!DOCTYPE html>
<html>

<head>
  <title>ScalarField / Interpolation</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <link rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
  <link rel="stylesheet" href="examples.css" />

  <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
</head>

<body>
  <h1 class="title mapTitle">ScalarField / Interpolation</h1>
  <div id="map"></div>

  <!-- CDN -->
  <script src="//d3js.org/d3.v4.min.js"></script>
  <script src="//npmcdn.com/leaflet@1.2.0/dist/leaflet.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js"></script>

  <!-- Plugin -->
  <script src="dist/leaflet.canvaslayer.field.js"></script>

  <script>
    let map = L.map('map');

    /* Basemap */
    var url = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png';
    L.tileLayer(url, {
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/attributions">CARTO</a>',
      subdomains: 'abcd'
    }).addTo(map);

    /* An ASCIIGrid Raster with Surface current velocity */
    d3.text('data/Bay_Speed.asc', function (asc) {
      let s = L.ScalarField.fromASCIIGrid(asc);

      let identify = function (e) {
        if (e.value !== null) {
          let v = e.value.toFixed(3);
          let html = `<span class="popupText">Surface currents velocity ${v} m/s</span>`;
          let popup = L.popup().setLatLng(e.latlng).setContent(html).openOn(map);
        }
      };

      // Bilinear interpolation
      let interpolated = L.canvasLayer.scalarField(s, {
        interpolate: true,
      });
      interpolated.on('click', identify);
      interpolated.addTo(map);

      // vs original
      let original = L.canvasLayer.scalarField(s);
      original.on('click', identify);
      map.fitBounds(interpolated.getBounds());


      L.control.layers({
        "Interpolated": interpolated,
        "Original": original
      }, {}, {
          position: 'bottomleft',
          collapsed: false
        }).addTo(map);


    });
  </script>
</body>

</html>